<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 20$this->user_id8/7/3$this->user_id 003$this->user_id
 * Time: $this->user_id6:43
 */

namespace app\api\controller;
use app\common\model\GiftRecord;
use app\common\model\Gift as model;
use think\Db;
use think\Exception;

class Gift extends  User
{
    /**
     * Created by xiaosong
     * E-mail:306027376@qq.com
     * 礼物列表
     */
    public function  giftList($type = false)
    {
        $model=new model();

        $map['status'] = 1;

        $rows = $model->giftList($map,$type,$this->user_id);

        api_return(1,'获取成功',$rows);

    }


    /**
     * 我的资产 -> 礼物 统计列表
     */
    public function myList()
    {
        $where['a.to_user'] = $this->user_id;
        $map['a.user_id']   = $this->user_id;
        $model = new GiftRecord();
        $rows['get']  = $model->giftCount($where);
        $rows['send'] = $model->giftCount($map);
        if (empty($rows['get']) && empty($rows['send'])) api_return(0,'暂无数据');
        api_return(1,'获取成功',$rows);
    }


    /**
     * 我收到|发出的礼物详情
     */
    public function giftDetail()
    {
        $where['a.gift_id'] = input('post.id');
        if (!empty(input('post.time'))){
            $time = input('post.time');
            $info = date_parse_from_format('Y-m',$time);
            if ( 0 != $info['warning_count'] || 0 != $info['error_count']) api_return(0,'时间错误');
            $start = strtotime($time);
            $end   = strtotime($time.'-'.date('t', strtotime($start)).' 23:59:59');
            $where['a.create_time'] = ['between time',[$start,$end]];
        }
        $type = input('post.type');
        if (!is_numeric($where['a.gift_id'])) api_return(0,'参数错误');
        if ($type == 1){
            $where['a.to_user'] = $this->user_id;
        }elseif ($type == 2){
            $where['a.user_id'] = $this->user_id;
        }else{
            api_return(0,'参数错误');
        }
        $model = new GiftRecord();
        $rows  = $model->myDetail($where,$type);
        if ($rows !== false) api_return(1,'获取成功',$rows);
        api_return(0,'暂无数据');

    }




    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * 钻石赠送礼物
     */
    public function giftGiving()
    {
        $data  = input('post.');
        $array = $this->giveGift($data,$this->user_id);
        $price = $array['price'];

        //查询背包数量
        $map['user_id'] = $this->user_id;
        $map['type']    = 1;
        $map['key']     = $array['id'];
        $num = Db::name('my_package')->where($map)->value('num')??0;


        Db::startTrans();
        try{
            //背包内还剩余礼物
            if ($num > 0){

                //背包足够赠送礼物
                if ($num >= $array['allNum']){
                    Db::name('my_package')->where($map)->setDec('num',$array['allNum']);
                }else{
                    //背包不够赠送礼物
                    Db::name('my_package')->where($map)->setDec('num',$num);
                    $number = $array['allNum'] - $num;
                    $money  = bcmul($number,$array['onePrice'],2);
                    $this->moneyDec($money,$this->user_id);
                }

            }else{
                $this->moneyDec($price,$this->user_id);
            }

            $this->gift($array,$this->user_id);
            $this->addLevel($array['price'],$this->user_id);
            Db::commit();
        }catch (Exception $e){
            Db::rollback();
            api_return(0,'系统错误',$e->getMessage());
        }

        api_return(1,'赠送成功');
    }

}